package view
{
	import com.greensock.TweenLite;
	
	import control.MonsterControl;
	
	import events.DamageToMonster;
	
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	
	public class Arrow extends Sprite
	{
		public function Arrow()
		{
			if(!arrow)
			{
				
				//画箭
				arrow = new Sprite();
				addChild(arrow);
				var g:Graphics = arrow.graphics;
				g.lineStyle(1,0x000000);
				g.moveTo(-2,0);
				g.lineTo(5,0);
				g.endFill();
				
				
			}
			
		}
		public var speed:uint = 500
		/**
		 *射程 
		 */		
		public var shottingRadius:uint = 200;
		/**
		 *发射起始点、当前塔的坐标 
		 */		
		public var start:Point = new Point();
		/**
		 *发射终点、目标怪物的坐标 
		 */		
		private var _end:Point = new Point();
		/**
		 * 箭
		 */		
		private var arrow:Sprite;
		/**
		 *最高攻击 
		 */		
		private var maxAttNum:Number = 5;
		/**
		 *最低攻击 
		 */		
		private var minAttNum:Number = 3;
		private var radian:Number;
		private var dy:Number;
		private var dx:Number;
		private var dist:Number;
		/**
		 *当前怪物 
		 */		
		public var monster:MonsterControl;
		/**
		 * 攻击力
		 */		
		private var attackNum:uint;
		/**
		 * 发射
		 * 
		 */		
		public function update():void
		{
			visible=true
			x = start.x;
			y = start.y;
			attackNum  = Math.random()*(maxAttNum-minAttNum)+minAttNum
			TweenLite.to(this,0.3,{x:end.x,y:end.y,onComplete:tweenCompleted});
			dx = end.x - x;
			dy = end.y - y;
			radian = Math.atan2(dy,dx);
			rotation = radian*180/Math.PI
			
		}
		/**
		 * 打到怪物触发事件
		 * 传出目标怪物
		 * 传出攻击力
		 * 
		 */		
		private function arrowed():void
		{
			if(monster.crtPoint){
			var evt:DamageToMonster = new DamageToMonster(DamageToMonster.DAMAGE_TO_MONSTER,monster,attackNum,true);
			dispatchEvent(evt);
			}
		}
		/**
		 *tween结束 
		 * 
		 */		
		private function tweenCompleted():void
		{
			
			arrowed();
			x= start.x;
			y= start.y;
			visible = false
		}
		
		public function get end():Point
		{
			return _end;
		}

		public function set end(value:Point):void
		{
			_end = value;
			update();
		}

	}
}